//
// LocalRepo
//
// Copyright (C) jextra.net.
//
//  This file is part of the net.jextra.dep Library.
//
//  The net.jextra.dep Library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The net.jextra.dep Library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the net.jextra.dep Library; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.dep.repo;

import java.io.*;
import java.util.*;
import net.jextra.dep.*;

/**
 * <p>
 * A {@link DepRepo} that represents the components in an actual development directory.
 * </p>
 */
public class LocalRepo implements DepRepo
{
    // ============================================================
    // Fields
    // ============================================================

    private File rootDir;
    private String depDefFileName;
    private HashMap<String, DepComponent> components;
    private HashMap<String, File> overrideDirs;

    // ============================================================
    // Constructors
    // ============================================================

    public LocalRepo( File rootDir, String depDefFileName )
    {
        assert rootDir != null : "rootDir can not be null";
        assert depDefFileName != null : "depDefFileName can not be null";

        this.rootDir = rootDir;
        this.depDefFileName = depDefFileName;
        components = new HashMap<String, DepComponent>();
        overrideDirs = new HashMap<String, File>();
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // public
    // ----------

    public void addComponent( DepComponent component )
        throws Exception
    {
        components.put( component.getName(), component );
        component.setRepo( this );
    }

    /**
     * Special addComponent that allows for a custom path not related to component name.
     */
    public void addComponent( DepComponent component, File overrideDir )
        throws Exception
    {
        components.put( component.getName(), component );
        overrideDirs.put( component.getName(), overrideDir );
        component.setRepo( this );
    }

    @Override
    public void init( DepRepoParam params )
        throws Exception
    {
        throw new DepException( "You can not instantiate a LocalRepo" );
    }

    @Override
    public String getName()
        throws Exception
    {
        return "<<SELF>>";
    }

    @Override
    public int getTimeout()
        throws Exception
    {
        return 0;
    }

    /**
     * All local components only have one version, 0.
     */
    @Override
    public List<DepRepoVersion> getVersions( String componentName )
        throws Exception
    {
        assert componentName != null : "componentName can not be null";

        DepComponent c = components.get( componentName );
        if ( c == null )
        {
            return new ArrayList<DepRepoVersion>();
        }

        ArrayList<DepRepoVersion> list = new ArrayList<DepRepoVersion>();
        list.add( new DepRepoVersion( this, componentName, "0" ) );

        return list;
    }

    @Override
    public String getLatestVersionNumber( String componentName )
        throws Exception
    {
        return "0";
    }

    @Override
    public String getNextVersionNumber( String componentName )
        throws Exception
    {
        return "0";
    }

    @Override
    public List<DepRepoFile> getFileEntries( String componentName, String versionNumber, String storePath )
        throws Exception
    {
        ArrayList<DepRepoFile> list = new ArrayList<DepRepoFile>();
        list.add( new DepRepoFile( this, componentName, versionNumber, depDefFileName, DepRepoFile.Type.file, 0L, 0L ) );

        return list;
    }

    @Override
    public DepRepoFile getFileEntry( String componentName, String versionNumber, String path )
        throws Exception
    {
        if ( !componentName.equalsIgnoreCase( depDefFileName ) )
        {
            return null;
        }

        return new DepRepoFile( this, componentName, versionNumber, depDefFileName, DepRepoFile.Type.file, 0L, 0L );
    }

    @Override
    public InputStream getFileEntryInputStream( String componentName, String versionNumber, String path )
        throws Exception
    {
        DepComponent component = components.get( componentName );
        if ( component == null )
        {
            return null;
        }

        File overrideDir = overrideDirs.get( componentName );
        if ( overrideDir != null )
        {
            File file = new File( overrideDir, path );

            return new FileInputStream( file );
        }
        else
        {
            File dir = new File( rootDir, component.getName() );
            File file = new File( dir, path );

            return new FileInputStream( file );
        }
    }

    @Override
    public String getComponentDisplayName( String componentName, String versionNumber )
        throws IOException
    {
        DepComponent component = components.get( componentName );
        if ( component == null )
        {
            return null;
        }

        File overrideDir = overrideDirs.get( componentName );
        if ( overrideDir != null )
        {
            return overrideDir.getCanonicalPath();
        }
        else
        {
            File dir = new File( rootDir, component.getName() );

            return dir.getCanonicalPath();
        }
    }

    @Override
    public void uploadFile( String componentName, String versionNumber, String storePath, File file )
        throws Exception
    {
        throw new DepException( "You can not upload to a SelfRepo" );
    }

    public File getFile( String componentName, String versionNumber, String path )
        throws IOException
    {
        DepComponent component = components.get( componentName );
        if ( component == null )
        {
            return null;
        }

        File overrideDir = overrideDirs.get( componentName );
        if ( overrideDir != null )
        {
            File file = new File( overrideDir, path );

            return file;
        }
        else
        {
            File dir = new File( rootDir, component.getName() );
            File file = new File( dir, path );

            return file;
        }
    }
}
